package com.pig4cloud.pig.module.controller.survey;

import cn.hutool.json.JSONArray;
import cn.hutool.json.JSONUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.pig4cloud.pig.common.core.util.R;
import com.pig4cloud.pig.common.log.annotation.SysLog;
import com.pig4cloud.pig.common.mybatis.page.PageUtils;
import com.pig4cloud.pig.common.mybatis.page.TableDataInfo;
import com.pig4cloud.pig.module.api.entity.survey.Survey;
import com.pig4cloud.pig.module.api.entity.survey.SurveyQuestion;
import com.pig4cloud.pig.module.service.survey.ISurveyQuestionService;
import com.pig4cloud.pig.module.service.survey.ISurveyService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.RequiredArgsConstructor;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.validation.Valid;
import javax.validation.constraints.NotEmpty;
import javax.validation.constraints.NotNull;
import java.util.Arrays;
import java.util.List;

/**
 * 问卷调查问题Controller
 *
 * @author ruoyi
 * @date 2021-09-05
 */
@Validated
@Api(value = "问卷调查问题控制器", tags = {"问卷调查问题管理"})
@RequiredArgsConstructor(onConstructor_ = @Autowired)
@RestController
@RequestMapping("/survey/question")
public class SurveyQuestionController {

    private final ISurveyService surveyService;
    private final ISurveyQuestionService surveyQuestionService;

    /**
     * 查询问卷调查问题列表
     */
    @ApiOperation("查询问卷调查问题列表")
    @PreAuthorize("@pms.hasPermission('module:question:list')")
    @GetMapping("/list")
    public TableDataInfo<SurveyQuestion> list(@Validated SurveyQuestion question) {
        QueryWrapper<SurveyQuestion> qw = new QueryWrapper<>();
        if(question.getSurveyId()!=null){
            qw.eq("survey_id",question.getSurveyId());
        }
        qw.orderByAsc("sort");
        return PageUtils.buildDataInfo(surveyQuestionService.page(PageUtils.buildPage(),qw));
    }

    /**
     * 导出问卷调查问题列表
     */
    @ApiOperation("导出问卷调查问题列表")
    @PreAuthorize("@pms.hasPermission('module:question:export')")
    @SysLog("问卷调查问题")
    @GetMapping("/export")
    public List export(@Validated SurveyQuestion surveyQuestion) {
        List<SurveyQuestion> list = surveyQuestionService.list();
        // ExcelUtil<SurveyQuestion> util = new // ExcelUtil<SurveyQuestion>(SurveyQuestion.class);
        return  list;// util.exportExcel(list, "问卷调查问题");
    }

    /**
     * 获取问卷调查问题详细信息
     */
    @ApiOperation("获取问卷调查问题详细信息")
    @PreAuthorize("@pms.hasPermission('module:question:query')")
    @GetMapping("/{id}")
    public R<SurveyQuestion> getInfo(@NotNull(message = "主键不能为空") @PathVariable("id") Integer id) {
        SurveyQuestion question = surveyQuestionService.getById(id);
        if(question!=null){
            if(question.getQuestionType() == 0){
                JSONArray jsonArray = JSONUtil.parseArray(question.getAnswerSection());
                question.setSections(jsonArray);
            }
        }
        return R.ok(question);
    }

    /**
     * 新增问卷调查问题
     */
    @ApiOperation("新增问卷调查问题")
    @PreAuthorize("@pms.hasPermission('module:question:add')")
    @SysLog("问卷调查问题")

    @PostMapping()
    public R<Void> add(@Valid @RequestBody SurveyQuestion surveyQuestion) {
        Survey survey = surveyService.getById(surveyQuestion.getSurveyId());
        if(survey!=null){
            surveyQuestion.setPropertyId(survey.getPropertyId());
            surveyQuestion.setQuestionTitle(survey.getQuestionTitle());
        }
        if(surveyQuestion.getQuestionType() == 1){
            surveyQuestion.setAnswerSection(null);
        }
        return R.okByRow(surveyQuestionService.save(surveyQuestion) ? 1 : 0);
    }

    /**
     * 修改问卷调查问题
     */
    @ApiOperation("修改问卷调查问题")
    @PreAuthorize("@pms.hasPermission('module:question:edit')")
    @SysLog("问卷调查问题")

    @PutMapping()
    public R<Void> edit(@Valid @RequestBody SurveyQuestion surveyQuestion) {
        Survey survey = surveyService.getById(surveyQuestion.getSurveyId());
        if(survey!=null){
            surveyQuestion.setPropertyId(survey.getPropertyId());
            surveyQuestion.setQuestionTitle(survey.getQuestionTitle());
        }
        if(surveyQuestion.getQuestionType() == 1){
            surveyQuestion.setAnswerSection(null);
        }
        return R.okByRow(surveyQuestionService.updateById(surveyQuestion) ? 1 : 0);
    }

    /**
     * 删除问卷调查问题
     */
    @ApiOperation("删除问卷调查问题")
    @PreAuthorize("@pms.hasPermission('module:question:remove')")
    @SysLog("问卷调查问题")
    @DeleteMapping("/{ids}")
    public R<Void> remove(@NotEmpty(message = "主键不能为空")  @PathVariable Long[] ids) {
        return R.okByRow(surveyQuestionService.removeByIds(Arrays.asList(ids)) ? 1 : 0);
    }
}
